/**
 * 
 */
package com.wixc.util;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.wixc.entity.Model;
import com.wixc.entity.NewBean;

/**
 * @author weichuanxi
 * @date 2016年4月8日
 */
public class JdbcUtils {
	private static Logger log = Logger.getLogger(JdbcUtils.class);

	// 数据库用户名
	private static final String USERNAME = "root";
	// 数据库密码
	private static final String PASSWORD = "root";
	// 驱动信息
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	// 数据库地址
	private static final String URL = "jdbc:mysql://localhost:3306/wixctest?useUnicode=true&characterEncoding=utf8";
	private static Connection connection;
	private PreparedStatement pstmt;
	public JdbcUtils() {
		// TODO Auto-generated constructor stub
		try {
			Class.forName(DRIVER);
			log.info("数据库连接成功！");

		} catch (Exception e) {

		}
	}

	/**
	 * 获得数据库的连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			connection = (Connection) DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public boolean updateByPreparedStatement(String sql, List<Object> params) throws SQLException {
		boolean flag = false;
		int result = -1;
		pstmt = (PreparedStatement) connection.prepareStatement(sql);
		int index = 1;
		if (params != null && !params.isEmpty()) {
			for (int i = 0; i < params.size(); i++) {
				pstmt.setObject(index++, params.get(i));
			}
		}
		result = pstmt.executeUpdate();
		flag = result > 0 ? true : false;
		return flag;
	}

	
	public static void saveNews(List<NewBean> news, String sql) throws SQLException {
		 PreparedStatement	ps = (PreparedStatement) getConnection().prepareStatement(sql);
		final int batchSize = 50;
		int count = 0;
		
		for (int i = 0; i < news.size(); i++) {
			ps.setInt(1, news.get(i).getId());
			ps.setString(2, news.get(i).getTitle());
			ps.setString(3, news.get(i).getDetail());
			ps.setString(4, news.get(i).getSource());
			ps.setString(5, news.get(i).getImageUrl());
			ps.setInt(6, news.get(i).getTypeId());
			ps.setDate(7,  news.get(i).getInsertTime());
			ps.addBatch();
			if (++count % batchSize == 0) {
				ps.executeBatch();
			}
		}
		ps.executeBatch(); // insert remaining records
		ps.close();
		connection.close();
	}

	public static void main(String[] args) throws SQLException {
	}

	/**
	 * @param model
	 * @param string
	 * @throws SQLException 
	 */
	public static void save(List<Model> model, String sql) throws SQLException {
		 PreparedStatement	ps = (PreparedStatement) getConnection().prepareStatement(sql);
			final int batchSize = 50;
			int count = 0;
			
			for (int i = 0; i < model.size(); i++) {
				ps.setInt(1, model.get(i).getId());
				ps.setString(2, model.get(i).getName());
				ps.setString(3, model.get(i).getDetail());
				ps.addBatch();
				if (++count % batchSize == 0) {
					ps.executeBatch();
				}
			}
			ps.executeBatch(); // insert remaining records
			ps.close();
			connection.close();
	}
}
